/**
 * 组件安装
 * npm install
 */

// 引入 gulp及组件
var gulp = require('gulp'), //基础库
    gulpSequence = require('gulp-sequence'),
    clean = require('gulp-clean'), //清空文件夹
    cheerio = require('gulp-cheerio'),
    replace = require('gulp-replace'),
    gulpif = require('gulp-if'),
    rename = require('gulp-rename'), //重命名
    del = require('del'),
    browserSync = require('browser-sync'),
    run = require('gulp-run');
var srcDst = '../dist/vuepages/';
var relDst = '../dist/vuepages/';

var opts;
var CDN = function () {
    var temp = false;
    if (!!opts.cdnUrl) {
        temp = true;
    }
    return temp;
};
// 清空图片、样式、js
gulp.task('clean', function () {
    return gulp.src([relDst], {
        read: false
    })
        .pipe(clean({
            force: true
        }));
});
gulp.task('build', function () {
    return run('npm run build').exec();
});

gulp.task('replaceCDN', function () {
    return gulp.src(srcDst + '/**/*.html')
        .pipe(gulpif(CDN, replace('href="./', 'href="' + opts.cdnUrl)))
        .pipe(gulpif(CDN, replace('src="./', 'src="' + opts.cdnUrl)))
        .pipe(gulp.dest(relDst))
});

//获取配置文件
gulp.task('del:config', function (cb) {
    return del(['./src/base/profile/config.json'], cb);
});
gulp.task('getOptsDev', ['del:config'], function () {
    opts = require('./profile/dev.json');
    return gulp.src('./profile/dev.json')
        .pipe(rename({
            basename: 'config'
        }))
        .pipe(gulp.dest('./src/base/profile/'))
});
gulp.task('getOptsIdc_product', ['del:config'], function () {
    opts = require('./profile/idc_product.json');
    return gulp.src('./profile/idc_product.json')
        .pipe(rename({
            basename: 'config'
        }))
        .pipe(gulp.dest('./src/base/profile/'))
});
gulp.task('getOptsProd', ['del:config'], function () {
    opts = require('./profile/prod.json');
    return gulp.src('./profile/prod.json')
        .pipe(rename({
            basename: 'config'
        }))
        .pipe(gulp.dest('./src/base/profile/'))
});
gulp.task('getOptsTest', ['del:config'], function () {
    opts = require('./profile/test.json');
    return gulp.src('./profile/test.json')
        .pipe(rename({
            basename: 'config'
        }))
        .pipe(gulp.dest('./src/base/profile/'))
});


// formart
var format = require('./formart.js');
gulp.task('formart:other', function () {
    return gulp
        .src(['./src/**/*.js', './src/**/*.scss', './src/**/*.less', '!./src/**/*.min.*','!./src/**/dingtalk_1_6_9.*'])
        .pipe(format())
        .pipe(gulp.dest('./src/'));
});
var prettier = require('gulp-plugin-prettier');
gulp.task('formart:vue', () => gulp.src(['./src/**/*.vue']).pipe(prettier.format({
    useTabs: true,
    tabWidth: 4,
    printWidth: 20000,
    singleQuote: true,
    trailingComma: 'es5',
    bracketSpacing: false,
    semi: true,
    requirePragma: false,
    arrowParens: 'always',
    proseWrap: 'always'
})).pipe(gulp.dest(file => file.base)));
gulp.task('formart', gulpSequence('formart:vue', 'formart:other'));






gulp.task('server',function() {
    browserSync({
        server: {
            //指定服务器启动根目录
            baseDir: relDst
        },
        port: 9997
    });
});
gulp.task('s',gulpSequence('server'));

gulp.task('default', gulpSequence("clean", "getOptsIdc_product", "build", 'replaceCDN'));
gulp.task('prod', gulpSequence("clean", "getOptsProd", "build", 'replaceCDN'));
gulp.task('dev', gulpSequence("clean", "getOptsDev", "build", 'replaceCDN'));
gulp.task('test', gulpSequence("clean", "getOptsTest", "build", 'replaceCDN'));

gulp.task('buildDevBefore', gulpSequence("clean", "getOptsDev"));
gulp.task('buildTestBefore', gulpSequence("clean", "getOptsTest"));
gulp.task('buildProdBefore', gulpSequence("clean", "getOptsProd"));

gulp.task('buildDevAfter', gulpSequence("getOptsDev", 'replaceCDN'));
gulp.task('buildTestAfter', gulpSequence("getOptsTest", 'replaceCDN'));
gulp.task('buildProdAfter', gulpSequence("getOptsProd", 'replaceCDN'));
